//
//  TransitionAnimationCustom.m
//  Home
//
//  Created by 曹连华 on 2019/7/3.
//  Copyright © 2019 曹连华. All rights reserved.
//

#import "TransitionAnimationCustom.h"

@interface TransitionAnimationCustom ()
@property (nonatomic, assign) AnimationType animationType;
@property (nonatomic, strong) NSDictionary *transitionParam;
@end

@implementation TransitionAnimationCustom

+ (instancetype)animationType:(AnimationType)aType {
    return [TransitionAnimationCustom animationType:aType params:@{}];
}

+ (instancetype)animationType:(AnimationType)aType params:(nullable NSDictionary *)params {
    return [[TransitionAnimationCustom alloc]initWithAnimationType:aType params:params];
}

- (instancetype)initWithAnimationType:(AnimationType)aType params:(nullable NSDictionary *)params {
    self = [super init];
    if (self) {
        self.animationType = aType;
        self.transitionParam = params;
    }
    return self;
}

- (void)push_pop_animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    switch (self.animationType) {
            case AnimationTypeSmallInBigOut:
            [self smallInBigOutAnimateTransition:transitionContext];
            break;
            
            case AnimationTypeSmallRoundInBigRoundOut:
            [self smallRoundInBigRoundOutAnimateTransition:transitionContext];
            break;
        default:
            [super push_pop_animateTransition:transitionContext];
            break;
    }
    
}

- (void)present_dismissed_animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    switch (self.animationType) {
            case AnimationTypeSmallInBigOut:
            [self smallInBigOutAnimateTransition:transitionContext];
            break;
            
            case AnimationTypeSmallRoundInBigRoundOut:
            [self smallRoundInBigRoundOutAnimateTransition:transitionContext];
            break;
        default:
            [super present_dismissed_animateTransition:transitionContext];
            break;
    }
}

- (void)smallInBigOutAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toViewController.view];
    [containerView addSubview:fromViewController.view];
    
    
    CABasicAnimation* animation = [self transitionBasicAnimationPath:@"transform.scale"];
    animation.delegate = [AnimationDelegate animationDidStartBlock:nil animationDidStop:^(CAAnimation * animation, BOOL flag) {
        //tbc的bug，所以得重新刷新界面，不然tabbar会有问题
        [toViewController.tabBarController.view setNeedsLayout];
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        [toViewController.view.layer removeAllAnimations];
        [fromViewController.view.layer removeAllAnimations];
    }];
    
    if (self.transitionType == TransitionTypePush
        || self.transitionType == TransitionTypePresent) {
        [containerView bringSubviewToFront:toViewController.view];
        animation.fromValue = @(0);
        animation.toValue = @(1);
        [toViewController.view.layer addAnimation:animation forKey:@"ToAnimationTypeSmallInBigOut"];
    } else {
        [containerView bringSubviewToFront:fromViewController.view];
        animation.fromValue = @(1);
        animation.toValue = @(0);
        [fromViewController.view.layer addAnimation:animation forKey:@"FromAnimationTypeSmallInBigOut"];
    }
}

- (void)smallRoundInBigRoundOutAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toViewController.view];
    [containerView addSubview:fromViewController.view];
    
    
    CAAnimationGroup *group = [self transitionGroupAnimation];
    CABasicAnimation* sizeAnimation = [self transitionBasicAnimationPath:@"bounds.size"];
    CABasicAnimation* roundAnimation = [self transitionBasicAnimationPath:@"cornerRadius"];
    group.animations = @[sizeAnimation,roundAnimation];
    group.delegate = [AnimationDelegate animationDidStartBlock:nil animationDidStop:^(CAAnimation * animation, BOOL flag) {
        //tbc的bug，所以得重新刷新界面，不然tabbar会有问题
        [toViewController.tabBarController.view setNeedsLayout];
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        [toViewController.view.layer removeAllAnimations];
        [fromViewController.view.layer removeAllAnimations];
    }];
    
    if (self.transitionType == TransitionTypePush
        || self.transitionType == TransitionTypePresent) {
        [containerView bringSubviewToFront:toViewController.view];
        sizeAnimation.fromValue = [NSValue valueWithCGSize:CGSizeMake(40, 40)];
        sizeAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(toViewController.view.bounds.size.width*2, toViewController.view.bounds.size.width*2)];
        roundAnimation.fromValue = @(20);
        roundAnimation.toValue = @(toViewController.view.bounds.size.width);
        [toViewController.view.layer addAnimation:group forKey:@"ToAnimationRoundTypeSmallInBigOut"];
    } else {
        [containerView bringSubviewToFront:fromViewController.view];
        sizeAnimation.fromValue = [NSValue valueWithCGSize:CGSizeMake(toViewController.view.bounds.size.width*2, toViewController.view.bounds.size.width*2)];
        sizeAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(40, 40)];
        roundAnimation.fromValue = @(toViewController.view.bounds.size.width);
        roundAnimation.toValue = @(20);
        [fromViewController.view.layer addAnimation:group forKey:@"FromAnimationRoundTypeSmallInBigOut"];
    }
    
}

- (void)dealloc {
    NSLog(@"TransitionAnimationCustom - dealloc ");
}
@end
